package com.chunfeng.lerunapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.chunfeng.lerunapplication.api.UserApi;
import com.chunfeng.lerunapplication.entity.JsonRequest;

/**
 * 注册
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    /**
     * 用户名
     */
    private EditText username;
    /**
     * 用户名校验
     */
    private TextView usernameMassage;
    /**
     * 密码
     */
    private EditText password;
    /**
     * 密码校验
     */
    private TextView passwordMessage;
    /**
     * 确认密码
     */
    private EditText aPassword;
    /**
     * 确认密码校验
     */
    private TextView aPasswordMessage;
    /**
     * 取消按钮
     */
    private Button cancel;
    /**
     * 注册按钮
     */
    private Button register;

    /**
     * 导入用户API
     */
    private UserApi userApi = new UserApi(this);

    /**
     * 初始化组件
     *
     * @param savedInstanceState 组件资源
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    /**
     * 初始化组件
     */
    private void init() {
        username = findViewById(R.id.Register_UserName);
        password = findViewById(R.id.Register_Password);
        aPassword = findViewById(R.id.Register_APassword);
        register = findViewById(R.id.Register_Commit);
        cancel = findViewById(R.id.Register);
        usernameMassage = findViewById(R.id.Register_Name_Message);
        passwordMessage = findViewById(R.id.Register_Password_Message);
        aPasswordMessage = findViewById(R.id.Register_APassword_Message);
    }

    /**
     * 按钮点击事件
     *
     * @param v 点击的按钮
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.Register_Commit) {
            register();
        } else if (id == R.id.Register_Cancel) {
            finish();
        }
    }

    /**
     * 注册逻辑
     */
    private void register() {
        String username = this.username.getText().toString();
        String password = this.password.getText().toString();
        String aPassword = this.aPassword.getText().toString();
        usernameMassage.setVisibility(username.isEmpty() ? View.VISIBLE : View.INVISIBLE);
        passwordMessage.setVisibility(password.isEmpty() || password.length() < 8 ? View.VISIBLE : View.INVISIBLE);
        aPasswordMessage.setVisibility(
                password.isEmpty() || aPassword.isEmpty() || !password.equals(aPassword)
                        ? View.VISIBLE : View.INVISIBLE);
        if (username.isEmpty()
                || password.isEmpty() || password.length() < 8
                || aPassword.isEmpty() || !password.equals(aPassword)) {
            return;
        }
        JsonRequest<Integer> request = userApi.register(username, password);
        if (request != null && request.getSuccess()) {
            Toast.makeText(this, "注册成功!请登录!", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "注册失败!", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 监听返回键
     */
    @Override
    public void onBackPressed() {
        finish();
    }
}